#region Copyright (C) 2005-2007 Team MediaPortal

/* 
 *	Copyright (C) 2005-2007 Team MediaPortal
 *	http://www.team-mediaportal.com
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

using System;
using System.Collections.Generic;
using System.Text;
using MediaPortal.GUI.Library;
using MediaPortal.Search;
using System.Windows.Forms;
using MediaPortal.Playlists;
using MediaPortal.Music.Database;
using MediaPortal.Dialogs;
using MediaPortal.Util;

namespace MediaPortal.GUI.GlobalSearch
{
  public class GUIGlobalMusicSearchDetail : GUIWindow  {
    #region Constants
    public const int UID = 30886;
    public const string GlobalSearchPluginName = "Search music";
    #endregion

    #region Private declarations
    [SkinControlAttribute(1)]
    protected GUIFadeLabel lblArtist = null;
    [SkinControlAttribute(2)]
    protected GUIFadeLabel lblAlbum = null;
    [SkinControlAttribute(3)]
    protected GUIFadeLabel lblSongname = null;
    [SkinControlAttribute(4)]
    protected GUIFadeLabel lblGenre = null;
    [SkinControlAttribute(5)]
    protected GUIFadeLabel lblYear = null;
    [SkinControlAttribute(6)]
    protected GUIFadeLabel lblFilename = null;
    [SkinControlAttribute(7)]
    protected GUIImage imgCoverArt = null;
    [SkinControlAttribute(8)]
    protected GUIFadeLabel lblTimesPlayed = null;
    [SkinControlAttribute(9)]
    protected GUIFadeLabel lblDuration = null;
    [SkinControlAttribute(10)]
    protected GUIFadeLabel lblDateTimePlayed = null;
    [SkinControlAttribute(11)]
    protected GUIButtonControl btnPlayNow = null;
    [SkinControlAttribute(12)]
    protected GUIButtonControl btnPlayNext = null;
    [SkinControlAttribute(13)]
    protected GUIButtonControl btnAddToPlayList = null;
    [SkinControlAttribute(14)]
    protected GUIButtonControl btnAddAlbumToPlaylist = null;
    [SkinControlAttribute(15)]
    protected GUIButtonControl btnAddToFavorites = null;

    private Song currentSong = null;
    #endregion

    #region overrides
    public void SetSong(Song song) 
    {
      this.currentSong = song;
      lblArtist.Label = song.Artist;
      lblAlbum.Label = song.Album;
      lblSongname.Label = song.Title;
      lblGenre.Label = song.Genre;
      lblYear.Label = song.Year.ToString();
      lblFilename.Label = song.FileName;
      lblDateTimePlayed.Label = (((song.TimesPlayed==0)||(song.DateTimePlayed.Year < 1900))?MediaPortal.GUI.Library.GUILocalizeStrings.Get(512):song.DateTimePlayed.ToString());
      lblDuration.Label = MediaPortal.Util.Utils.SecondsToHMSString(song.Duration);
      lblTimesPlayed.Label = song.TimesPlayed.ToString();

      GUIPropertyManager.SetProperty("#SongDetails.Rating", (Convert.ToDecimal(2 * song.Rating + 1)).ToString());

      PlayListPlayer playlistPlayer = PlayListPlayer.SingletonPlayer;
      PlayList playList = playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC);
      PlayListItem pli = new PlayListItem(song.Title, song.FileName, song.Duration);

      string smallCoverArt = MediaPortal.Util.Utils.GetCoverArt(System.IO.Path.GetDirectoryName(song.FileName), "folder");
      string largeCoverArt = MediaPortal.Util.Utils.GetLargeCoverArtName(System.IO.Path.GetDirectoryName(song.FileName), "folder");

      try
      {
        imgCoverArt.FreeResources();
        if (System.IO.File.Exists(smallCoverArt))
        {
          imgCoverArt.SetFileName(smallCoverArt);
        }
        else
        {
          if (System.IO.File.Exists(largeCoverArt))
          {
            imgCoverArt.SetFileName(largeCoverArt);
          }
          else
          {
            imgCoverArt.SetFileName(GUIGraphicsContext.Skin + @"\media\missing_coverart.png");
          }
        }
        
        imgCoverArt.AllocResources();
      } 
      catch (Exception e)
      {
        MediaPortal.GUI.Library.Log.Error(e);
      }

      lblArtist.Width = 272;
      lblAlbum.Width = 272;
      lblSongname.Width = 272;
      lblGenre.Width = 272;
      lblYear.Width = 272;
      lblFilename.Width = 272;
    }

    public override int GetID
    {
      get
      {
        return UID;
      }
      set
      {
        base.GetID = value;
      }
    }

    public override bool Init()
    {
      return Load(GUIGraphicsContext.Skin + @"\GlobalSearch.Music.Details.xml");
    }

    protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
    {
      if (control == btnPlayNow)
      {
        GUIGlobalMusicSearch.PlayNow(currentSong);
        GUIWindowManager.ShowPreviousWindow();
      } else
      if (control == btnPlayNext)
      {
        GUIGlobalMusicSearch.PlayNext(currentSong);
        GUIWindowManager.ShowPreviousWindow();
      }
      else
      if (control == btnAddToPlayList)
      {
        GUIGlobalMusicSearch.AddSongToPlaylist(currentSong);
        GUIWindowManager.ShowPreviousWindow();
      }
      else
      if (control == btnAddAlbumToPlaylist)
      {
        GUIGlobalMusicSearch.AddAlbumToPlayList(currentSong);
        GUIWindowManager.ShowPreviousWindow();
      }
      else
      if (control == btnAddToFavorites)
      {
        GUIGlobalMusicSearch.AddSongToFavorites(currentSong);
        GUIWindowManager.ShowPreviousWindow();
      }

      base.OnClicked(controlId, control, actionType);
    }
    #endregion
  }
}